package com.hx.demo.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hx.demo.domain.User;
import com.hx.demo.mapper.UserMapper;
import com.hx.demo.service.IUserSeervice;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * User 的实现类
 * 注入成一个Bean
 * @Component, @Repository, @Service  3个注解的功能是一致，各自的职责不一样
 * @Component 配置相关的类
 * @Service 服务层
 * @Repository 数据库层
 */
@Service
public class UserSeerviceImpl implements IUserSeervice {

    @Autowired
    private UserMapper userMapper;


    @Override
    public Page<User> selectUserListByPage(Page<User> page, User user) {

        if (ObjectUtil.isNull(user)){
            user = new User();
        }

        return userMapper.selectUserListByPage(page, user);
    }

    @Override
    public User selectById(Integer id) {
        return userMapper.selectById(id);
    }

    @Override
    public User selectByUserName(String userName) {

        //Wrapper<T>	queryWrapper	实体对象封装操作类 QueryWrapper
        QueryWrapper<User> queryWrapper = new QueryWrapper<User>();
        queryWrapper.eq("uname",userName);
        // select * from user where uname = "tongxing";
        // select * from user where uname = #{user.uname}
        User user = userMapper.selectOne(queryWrapper);
        return user;
    }

    @Override
    public int insertUser(User user) {
        return userMapper.insert(user);
    }

    @Override
    public int updateUser(User user) {
        return userMapper.updateById(user);
    }

    @Override
    public int deleteUserById(Integer id) {
        return userMapper.deleteById(id);
    }
}
